package com.example.mdoncall;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author Anthony Maniquiz & Laine Nicolas (Smart Telecom Inc. OJT) 
 * @version 1
 * 
 */

public class ViewPatientActivity extends Activity{
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_viewpatient);
		setOrientation();
		
		Intent intent = getIntent();
		int search = intent.getIntExtra("search",0);
		String[] info = intent.getStringArrayExtra("info");
		String[] medHistory = intent.getStringArrayExtra("medHistory");
		Boolean isNewPatient = intent.getBooleanExtra("newPatient", false);
		
		
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		
		TextView name = (TextView) findViewById(R.id.nameTxt);
		TextView gender = (TextView) findViewById(R.id.genderTxt);
		TextView bday = (TextView) findViewById(R.id.bdayTxt);
		TextView contact = (TextView) findViewById(R.id.contactTxt);
		
		TextView address = (TextView) findViewById(R.id.addressTxt);
		
		TextView condition = (TextView) findViewById(R.id.conditionTxt);
		TextView allergy = (TextView) findViewById(R.id.allergyTxt);
		TextView medication = (TextView) findViewById(R.id.medicationTxt);

		LinearLayout relatedPlist = (LinearLayout) findViewById(R.id.bodyLayout);
		
		int one = 0;
		int two = 0;
		
		int currentId = 0;
		String currentNum = null;
		
		Intent intentTwo = new Intent(this, SearchPatientActivity.class);
		
		if(isNewPatient == true){
			name.setText(info[0] + ", " + info[1] + " " + info[2]);
			gender.setText(info[3]);
			bday.setText(info[4]);
			contact.setText(info[5]);
			address.setText(info[6] + " " + info[7]);
			condition.setText(medHistory[0]);
			allergy.setText(medHistory[1]);
			medication.setText(medHistory[2]);
		}

		for(Contact cn: contacts) {
			if (cn.getID() == search) {
				one = cn.getID();
				two = cn.getID();
				currentId = cn.getID();
				currentNum = cn.getPhoneNumber();
			}
		}
		
		
		if (one == two) {
			
		
		for(Contact cn: contacts){
			
			if(cn.getID() == one) {
				name.setText(cn.getLname() + ", " + cn.getFname() + " " + cn.getMname());
				gender.setText(cn.getGender());
				bday.setText(cn.getBday());
				contact.setText(cn.getPhoneNumber());
				address.setText(cn.getStreet() + " " + cn.getCity() );
				condition.setText(cn.getCondition());
				allergy.setText(cn.getAllergy());
				medication.setText(cn.getMedication());
				}		
			
			if(cn.getPhoneNumber().equals(currentNum) && cn.getID() != currentId){
				TextView relatePtxt = (TextView) findViewById(R.id.relatedPTxt);
				relatePtxt.setText("");
				final int id = cn.getID();
				TextView  tv = new TextView(this);
				tv.setTextSize(15);
				tv.setId(id);
				tv.setPadding(5, 5, 5, 5);
				tv.setBackgroundResource(R.drawable.back);
				tv.setText(cn.getLname() + ", " + cn.getFname() );
				tv.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
					goSearch(v, id);
						}
					});
				relatedPlist.addView(tv);
				}	
		}
		} else {
			Toast.makeText(getApplicationContext(), "Patient is not Registered", Toast.LENGTH_SHORT).show();
			startActivity(intentTwo);
		}
		
		db.close();
		
	}
	
	public void goHome(View view){
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}
	
	public void editInfo(View view){
		Intent intent = new Intent(this, EditPatientActivity.class);	
		
		Intent intentTwo = getIntent();
		int search = intentTwo.getIntExtra("search", 0);
		
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		
		for(Contact cn: contacts){
			if(cn.getID() == search) {
				search = cn.getID();
			}
		}
		
		intent.putExtra("search", search);
		startActivity(intent);
	}
	
	public void goSearch(View view, int id){
		Intent intent = new Intent(this, ViewPatientActivity.class);
		
		intent.putExtra("search", id);
		startActivity(intent);
	}
	
	public void addPatient(View view){
		Intent intent = new Intent(this, PatientInfoActivity.class);
		startActivity(intent);
	}
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }

}
